﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;

namespace MLY.LTR
{
    public partial class UserInfo : Bll.BasePage
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Expires = 0;
            Response.Cache.SetNoStore();
            Response.AppendHeader("Pragma", "no-cache");



            if (!Page.IsPostBack)
            {
                ViewState["Referer"] = Request.ServerVariables["HTTP_REFERER"];


                int UserId = int.Parse(Session["UserId"].ToString());

                Bll.clsUser user = new MLY.Bll.clsUser(UserId);


                this.txtUserName.Text = user.Data.UserName;
                HelloText.Text = "Hello, " + user.Data.UserName;

                this.txtFullName.Text = user.Data.FullName;
                this.txtAddress.Text = user.Data.Address;
                this.txtEmail.Text = user.Data.Email;
                this.txtPassword.Text = user.Data.Password;
                this.txtPhone.Text = user.Data.Phone;
                if (user.Data.DefaultLinesinGrid.HasValue)
                    this.NumRowsInGridviewTextBox.Text = user.Data.DefaultLinesinGrid.Value.ToString();

                DropDownOrganization.DataBind();
                DropDownOrganization.SelectedValue = user.Data.OrganiztionId.ToString();
                DropDownRole.DataBind();
                DropDownRole.SelectedValue = user.Data.RollId.ToString();


            }
            succLabel.Visible = false;
        }

        protected void btnClose_Click(object sender, EventArgs e)
        {
            Response.RedirectTo((string)ViewState["Referer"]);
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            try
            {
                int UserId = int.Parse(Session["UserId"].ToString());
                Bll.clsUser user = new MLY.Bll.clsUser(UserId);
                user.Data.UserName = txtUserName.Text;
                user.Data.FullName = txtFullName.Text;
                user.Data.Address = txtAddress.Text;
                user.Data.Email = txtEmail.Text;
                user.Data.Password = txtPassword.Text;
                user.Data.Phone = txtPhone.Text;
                if (NumRowsInGridviewTextBox.Text.Trim().Length >= 1)
                {
                    int RowsinGrid;
                    if (int.TryParse(NumRowsInGridviewTextBox.Text.Trim(), out RowsinGrid))
                        user.Data.DefaultLinesinGrid = RowsinGrid;
                }    
                user.Save();
                succLabel.Visible = true;

            }
            catch (Exception ex)
            {
                errLbl.Text = ex.Message;
            }

        }
    }
}
